package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"strings"
)

func read(conn net.Conn) {
	for {

		b := make([]byte, 1024)
		n, err := conn.Read(b)
		if err != nil {
			fmt.Println("对话结束")
			fmt.Println(err)
		}
		fmt.Print(string(b[:n]))
	}

}

func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:8920")

	if err != nil {
		fmt.Println("连接而出错")
		fmt.Println(err)
		return
		// handle error
	}
	go read(conn)
	// fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
	for {

		status, err := bufio.NewReader(os.Stdin).ReadString('\n')
		if err != nil {
			fmt.Println("读出错")
			fmt.Println(err)
			return
		}
		if strings.Trim(status, " \r\n") == "exit" {
			fmt.Println("客户端退出")
			break
		}

		conn.Write([]byte(status))

	}

}
